package com.zengdada.authority.service.impl;

import com.zengdada.authority.config.security.intercept.AuthorityUtil;
import com.zengdada.authority.model.AppUser;
import com.zengdada.authority.model.Dml;
import com.zengdada.authority.service.DmlAuthorityService;
import com.zengdada.authority.service.EnhanceBaseService;
import com.zengdada.authority.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Auther: 12614
 * @Contact: 1261438511@qq.com
 * @Date: 2019-02-20 17:27
 * @Description:
 */
@Service
public class DmlAuthorityServiceImpl extends EnhanceBaseService<Dml> implements DmlAuthorityService {

    @Autowired
    AuthorityUtil authorityUtil;

    @Override
    public int save(Dml dml) {
        AppUser user = authorityUtil.getLoginUser();
        dml.setModifyUser(user.getId());
        dml.setModifyTime(new Date());
        if (StringUtils.isBlank(dml.getId())) {
            try {
                CommonUtils.reflectObjectTransString(dml, false);
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
            return this.add(dml);
        } else return this.updateById(dml);
    }
}
